package com.example.demo.observer;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

// 天气事件类
class WeatherEvent {
    private double temperature;
    private double humidity;
    private double pressure;

    public WeatherEvent(double temperature, double humidity, double pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
    }

    public double getTemperature() {
        return temperature;
    }

    public double getHumidity() {
        return humidity;
    }

    public double getPressure() {
        return pressure;
    }
}

// 气象站主题类
@Component
class WeatherData implements ApplicationEventPublisherAware {
    private ApplicationEventPublisher publisher;
    private double temperature;
    private double humidity;
    private double pressure;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }

    public void setMeasurements(double temperature, double humidity, double pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }

    private void measurementsChanged() {
        publisher.publishEvent(new WeatherEvent(temperature, humidity, pressure));
    }
}

// 显示设备观察者类
@Component
class CurrentConditionsDisplay {
    public void handleWeatherEvent(WeatherEvent event) {
        System.out.println("当前状况: 温度 " + event.getTemperature() + "F 度, 湿度 " + event.getHumidity() + "%");
    }
}    